using System;
using MONA.Modules.Marketing.BLL;

namespace MONA.Modules.Marketing.Controls
{
    public partial class AddEditServicePackage : AddEditItemControl
    {
        public event EventHandler SaveClick;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            UpdateBar.Visible = this.ShowUpdateBar;
            btnDelete.Visible = this.ItemID > 0;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (this.ItemID > 0)
                {
                    ServicePackage sp = ServicePackage.Load(this.ItemID);
                    if (sp != null)
                    {
                        txtName.Text = txtName.Text;
                        txtDescription.Text = sp.Description;
                        txtFromDate.Text = sp.FromDate.ToShortDateString();
                        txtToDate.Text = sp.ToDate.ToShortDateString();
                        txtPrice.Amount = sp.Price;
                        txtMinPrice.Amount = sp.MinPrice;
                        txtMaxPrice.Amount = sp.MaxPrice;
                    }
                }
            }
        }

        public bool Save()
        {
            int returnId = 0;
            if (Page.IsValid)
            {
                ServicePackage sp = new ServicePackage();
                sp.Name = txtName.Text.Trim();
                sp.Description = txtDescription.Text.Trim();
                sp.Price = txtPrice.Amount;
                sp.MinPrice = txtMinPrice.Amount;
                sp.MaxPrice = txtMaxPrice.Amount;
                sp.FromDate = Convert.ToDateTime(txtFromDate.Text);
                sp.ToDate = Convert.ToDateTime(txtFromDate.Text);

                if (this.ItemID == 0)
                {
                    returnId = sp.Insert();
                }
                else
                {
                    sp.ID = this.ItemID;
                    returnId = sp.Update();
                }
                // Clear data in controls.
                txtName.Text = txtDescription.Text = txtFromDate.Text = txtToDate.Text = string.Empty;
                txtPrice.Amount = txtMinPrice.Amount = txtMaxPrice.Amount = 0;
            }
            return returnId > 0;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.Save();

            if (SaveClick != null)
            {
                SaveClick(sender, e);
            }
            this.ReturnToReferrerPage();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.ReturnToReferrerPage();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                ServicePackage sp = new ServicePackage();
                sp.ID = this.ItemID;
                sp.Delete();
            }
        }
    }
}